package com.simple.util.math.function;

/**
 * @program: simple_tools
 * @description: 对数函数
 * @author: ChenWenLong
 * @create: 2019-10-24 19:57
 **/
public class LogFunction {

    //y=logaX （a>0，且a≠1）
    private double a = 2;
    private static final Point DEFAULT_POINT = new Point(1,0);
    public static LogFunction instance;

    private LogFunction() {
    }

    static {
        if (instance == null) {
            synchronized (LogFunction.class) {
                if (instance == null) {
                    instance = new LogFunction();
                }
            }
        }
    }

    /**
     * 功能描述:
     * 〈初始化对数函数〉
     *
     * @return : void
     * @params : [a]
     * @author : cwl
     * @date : 2019/10/25 9:11
     */
    public static void init(double a) {
        if (a < 0 || a == 1) {
            throw new RuntimeException("a is not less than zero and not be one");
        }
        instance.setA(a);
    }

    /**
     * 功能描述:
     * 〈判断点是否在对数函数上〉
     *
     * @params : [point]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/25 9:18
     */
    public static boolean isOnline(Point point) {
        if (point == null){
            throw new RuntimeException("point is not be null");
        }
        double x = point.getX();
        double y = point.getY();
        return y == Math.pow(x,instance.getA());
    }

    /**
     * 功能描述:
     * 〈每个对数函数都会过点(1,0)〉
     *
     * @params : []
     * @return : com.simple.util.math.function.LogFunction.Point
     * @author : cwl
     * @date : 2019/10/25 9:23
     */
    public static Point getDefaultPoint(){
        return DEFAULT_POINT;
    }

    // 二维函数图上的点
    public static class Point {
        // 坐标 x
        private double x;
        // 坐标 y
        private double y;

        public Point(double x,double y){
            this.x = x;
            this.y = y;
        }

        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }
}
